<?php
if (! defined ( 'IN_JISHIGOU' )) {
	exit ( 'invalid request' );
}
function api_output($result, $status = '', $code = 0) {
	$outputs = array ();
	if ($status) {
		$outputs ['status'] = $status;
		$outputs [$status] = true;
	}
	if ($code) {
		$outputs ['code'] = "{$code}";
	} else {
		$outputs ['code'] = '200';
	}
	$outputs ['result'] = $result;
	if (jget ( 'debug_input' )) {
		$outputs ['DEBUG_INPUT_G'] = $_GET;
		$outputs ['DEBUG_INPUT_P'] = $_POST;
		$outputs ['DEBUG_INPUT_C'] = $_COOKIE;
		$outputs ['DEBUG_PHP_INPUT'] = file_get_contents ( 'php:/' . '/input' );
	}
	if (jget ( 'debug_server' )) {
		$outputs ['DEBUG_SERVER'] = $_SERVER;
	}
	$outputs = array_iconv ( $GLOBALS ['_J'] ['charset'], 'utf-8', $outputs );
	ob_clean ();
	if ('json' == API_OUTPUT) {
		$api_output_json_header = ( bool ) (isset ( $_POST ['__API__'] ['output_json_header'] ) ? $_POST ['__API__'] ['output_json_header'] : $_GET ['__API__'] ['output_json_header']);
		if ($api_output_json_header) {
			header ( "Content-type: application/json;charset=utf-8" );
		} else {
			header ( "Content-type: text/html;charset=utf-8" );
		}
		$outputs_json = json_encode ( $outputs );
		$api_output_jsonp_callback = (isset ( $_POST ['__API__'] ['output_jsonp_callback'] ) ? $_POST ['__API__'] ['output_jsonp_callback'] : $_GET ['__API__'] ['output_jsonp_callback']);
		if ($api_output_jsonp_callback) {
			echo $api_output_jsonp_callback . '(' . $outputs_json . ');';
		} else {
			echo $outputs_json;
		}
	} elseif ('serialize_base64' == API_OUTPUT) {
		header ( "Content-type: text/html;charset=utf-8" );
		echo base64_encode ( serialize ( $outputs ) );
	} else {
		$api_output_xml_htmlon = ( bool ) (isset ( $_POST ['__API__'] ['output_xml_htmlon'] ) ? $_POST ['__API__'] ['output_xml_htmlon'] : $_GET ['__API__'] ['output_xml_htmlon']);
		$api_output_xml_isnormal = ( bool ) (isset ( $_POST ['__API__'] ['output_xml_isnormal'] ) ? $_POST ['__API__'] ['output_xml_isnormal'] : $_GET ['__API__'] ['output_xml_isnormal']);
		header ( "Content-type: application/xml;charset=utf-8" );
		echo xml_serialize ( $outputs, $api_output_xml_htmlon, $api_output_xml_isnormal );
	}
}
function api_error($msg, $code = 0, $halt = true) {
	api_output ( $msg, 'error', $code );
	$halt && exit ();
}
function api_is_special_chars($v) {
	$cs = array (
			'<',
			'>',
			'&',
			'"',
			"'" 
	);
	foreach ( $cs as $c ) {
		if (false !== strpos ( $v, $c )) {
			return true;
		}
	}
	return false;
}
if (! function_exists ( 'xml_unserialize' )) {
	function xml_unserialize(&$xml, $isnormal = FALSE) {
		$xml_parser = new XML ( $isnormal );
		$data = $xml_parser->parse ( $xml );
		$xml_parser->destruct ();
		return $data;
	}
}
if (! function_exists ( 'xml_serialize' )) {
	function xml_serialize($arr, $htmlon = false, $isnormal = false, $level = 1) {
		$s = ($level == 1 ? "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>\r\n" : '');
		$space = str_repeat ( "\t", $level );
		foreach ( $arr as $k => $v ) {
			if (! is_array ( $v )) {
				$html = ($htmlon ? true : (is_numeric ( $v ) ? false : api_is_special_chars ( $v )));
				$s .= $space . ($isnormal ? "<$k>" : "<item id=\"$k\">") . ($html ? '<![CDATA[' : '') . $v . ($html ? ']]>' : '') . ($isnormal ? "</$k>" : "</item>") . "\r\n";
			} else {
				$s .= $space . ($isnormal ? "<$k>" : "<item id=\"$k\">") . "\r\n" . xml_serialize ( $v, $htmlon, $isnormal, $level + 1 ) . $space . ($isnormal ? "</$k>" : "</item>") . "\r\n";
			}
		}
		$s = preg_replace ( "/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s );
		return ($level == 1 ? $s . "</root>" : $s);
	}
}
class XML {
	var $parser;
	var $document;
	var $stack;
	var $data;
	var $last_opened_tag;
	var $isnormal;
	var $attrs = array ();
	var $failed = FALSE;
	function __construct($isnormal) {
		$this->XML ( $isnormal );
	}
	function XML($isnormal) {
		$this->isnormal = $isnormal;
		$this->parser = xml_parser_create ( 'ISO-8859-1' );
		xml_parser_set_option ( $this->parser, XML_OPTION_CASE_FOLDING, false );
		xml_set_object ( $this->parser, $this );
		xml_set_element_handler ( $this->parser, 'open', 'close' );
		xml_set_character_data_handler ( $this->parser, 'data' );
	}
	function destruct() {
		xml_parser_free ( $this->parser );
	}
	function parse(&$data) {
		$this->document = array ();
		$this->stack = array ();
		return xml_parse ( $this->parser, $data, true ) && ! $this->failed ? $this->document : '';
	}
	function open(&$parser, $tag, $attributes) {
		$this->data = '';
		$this->failed = FALSE;
		if (! $this->isnormal) {
			if (isset ( $attributes ['id'] ) && ! is_string ( $this->document [$attributes ['id']] )) {
				$this->document = &$this->document [$attributes ['id']];
			} else {
				$this->failed = TRUE;
			}
		} else {
			if (! isset ( $this->document [$tag] ) || ! is_string ( $this->document [$tag] )) {
				$this->document = &$this->document [$tag];
			} else {
				$this->failed = TRUE;
			}
		}
		$this->stack [] = &$this->document;
		$this->last_opened_tag = $tag;
		$this->attrs = $attributes;
	}
	function data(&$parser, $data) {
		if ($this->last_opened_tag != NULL) {
			$this->data .= $data;
		}
	}
	function close(&$parser, $tag) {
		if ($this->last_opened_tag == $tag) {
			$this->document = $this->data;
			$this->last_opened_tag = NULL;
		}
		array_pop ( $this->stack );
		if ($this->stack) {
			$this->document = &$this->stack [count ( $this->stack ) - 1];
		}
	}
}
